package com.lipi.mall.cart.controller;

import com.lipi.common.constant.AuthServerConstant;
import com.lipi.common.utils.R;
import com.lipi.mall.cart.interceptor.CartInterceptor;
import com.lipi.mall.cart.service.CartService;
import com.lipi.mall.cart.vo.Cart;
import com.lipi.mall.cart.vo.CartItem;
import com.lipi.mall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;


    @GetMapping("/cart.html")
    public String cartList(Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }


    @GetMapping("/cart.action")
    public R cartAction() {
        return null;
    }

    @GetMapping("/success")
    public String success() {
        return "success";
    }


    /**
     * 更改购物项选中状态
     *
     * @param skuId skuId
     * @param check 选中状态,0：未选中,1：选中
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check) {
        cartService.checkItem(skuId, check);

        return "redirect:http://cart.mall.com/cart.html";
    }

    /**
     * 修改购物车中商品的数量
     *
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num) {
        cartService.changeItemCount(skuId, num);
        return "redirect:http://cart.mall.com/cart.html";
    }

    /**
     * 获取当前用户的所有checked状态购物项数据(update price form db)
     * @return
     */
    @ResponseBody
    @GetMapping("/getCurrentUserCartItems")
    public List<CartItem> getCurrentUserCartItems() {

        List<CartItem> res = cartService.getUserCartItems();
        return res;
    }

    /**
     * 删除购物车中的购物项
     *
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {

        cartService.deleteItem(skuId);
        return "redirect:http://cart.mall.com/cart.html";
    }


    /**
     * 添加指定商品到购物车
     *
     * @param skuId
     * @param num   需要添加到购物车的商品数量,累积式
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {

        CartItem item = cartService.addToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);

        return "redirect:http://cart.mall.com/addToCartSuccess.html";
    }


    /**
     * 购物车添加成功后跳转页面(实现接口幂等性)
     *
     * @param skuId
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        //查询展示信息
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItem);
        return "success";
    }

}
